﻿using System;
using System.Collections.Generic;
using System.Text;
using RS.DOM.Common;

namespace RS.DOM.Types
{
    public class MutableName: Name
    {
        public MutableName(Context context, string text)
            : base(context, text)
        {
            base.Text = text;
            base.TokenType = TokenType.MutableName;
        }

        public override Token Operation(Token token, OperationType type)
        {
            Mutable result = null;
            if (type == OperationType.Apply)
            {
                result = new Mutable(this.Context, base.Text);

                if (token.TokenType == TokenType.Collection)
                {
                    Collection op2 = (Collection)token;

                    foreach(Token op in op2.Result)
                    {
                        result.Result.Add(op);
                    }
                }
                
                this.Context.Document.AddVariable(base.Text, result);
            }
            else
            {
                base.Operation(token, type);
            }

            return result;
        }

        public override Token Create()
        {
            return new MutableName(this.Context, this.Text);
        }
    }
}
